package com.water.camera.kt

import com.snap.base.ktx.adNative
import com.snap.base.ktx.isChecking
import com.snap.base.ktx.requestLatestVer
import com.snap.base.ktx.setVipDesc
import com.snap.base.util.BaseUtil
import com.snap.base.vm.UserVM
import com.snap.common.databinding.view.ViewBindingAdapter.gone
import com.snap.common.main.BaseMVVMFg
import com.snap.util.appVerName
import com.snap.util.toast
import com.water.camera.R
import com.water.camera.databinding.FgMeBinding

class MeFg : BaseMVVMFg<FgMeBinding, MeVM>() {
    override fun createVM(): MeVM {
        return MeVM().apply {
            actionAct = { requireActivity() }
        }
    }

    override fun doDataBind() {
        binding.userVM = UserVM
        binding.viewModel = viewModel
    }

    override fun getContentViewId(): Int {
        return R.layout.fg_me
    }

    override fun onViewCreated() {
        checkAppVersion()
        BaseUtil.adNative(requireActivity(), binding.fl, adConfig = AdConfig.AD_NATIVE)
        BaseUtil.setVipDesc(this, binding.tvDesc)
    }

    override fun onResume() {
        super.onResume()
        changeVIPStatus()
    }

    private fun changeVIPStatus() {
        if (BaseUtil.isChecking()) {
            binding.imageVipStatus.gone(true)
            binding.imgVipBuy.gone(true)
        }
    }

    private fun checkAppVersion() {
        viewModel.actionUpdate = {
            showVersion(true)
        }
    }
    private suspend fun showVersion(isShowUpdate: Boolean = false) {
        BaseUtil.requestLatestVer(isShowUpdate, requireActivity()).apply {
            if (this) {
                viewModel.versionName.value = "发现新版本"
            } else {
                if (isShowUpdate) {
                    viewModel.versionName.value = "当前版本$appVerName"
                    toast("当前已是最新版本")
                }
            }
        }
    }
}